package cc.wanforme.chipmunity.system.service.sync;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import cc.wanforme.chipmunity.common.consts.PassageEnum;
import cc.wanforme.chipmunity.system.event.SectionEvent;
import cc.wanforme.chipmunity.system.mapper.PassageMapper;
import cc.wanforme.chipmunity.system.mapper.SubjectMapper;
import cc.wanforme.chipmunity.system.po.Section;
import cc.wanforme.chipmunity.system.po.Subject;

/**
 * Section改变后，同步事件
 * @author wanne
 * 2019年8月17日
 * 
 */
@Service
public class SectionSyncService implements ApplicationListener<SectionEvent>{

	// 删除 - 所有相关文章和回复的状态
	@Autowired
	private SubjectMapper subjectMapper;
	
	@Autowired
	private PassageMapper passageMapper;
	
	@Override
	public void onApplicationEvent(SectionEvent event) {
		Section section = (Section) event.getSource();
		
		switch(event.getType()) {
			case DELETE: 
			case UPDATE_DELETE: 
				List<Subject> subjects = subjectMapper.listSubjectBySection(section.getName());
				for (Subject subject : subjects) {
					passageMapper.updatePassagesState( subject.getId(), PassageEnum.STATE_HIDE.getState());
				}
				break;
			default:
				break;
		
		}
	}


}
